package com.hanserwei.springbootlearn.mapper;

import com.hanserwei.springbootlearn.entity.User;
import org.apache.ibatis.annotations.*;

@Mapper
public interface UserMapper {
    @Select("select * from user where id = #{id}")
    @Results({
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "name", property = "name"),
            @Result(column = "gender", property = "gender"),
            @Result(column = "user_information_id", property = "userInformation", one = @One(select = "com.hanserwei.springbootlearn.mapper.InformationMapper.selectUserInformationById")),
            @Result(column = "id", property = "roles", many = @Many(select = "com.hanserwei.springbootlearn.mapper.RoleMapper.selectUserRoles"))
    })
    User selectUserById(Integer id);

    @Insert("insert into user (name, gender,user_information_id) values (#{user.name}, #{user.gender},#{userInformationId})")
    @Options(useGeneratedKeys = true, keyColumn = "id", keyProperty = "user.id")
    int insertUser(@Param("user") User user, @Param("userInformationId") Integer userInformationId);

    @Update("update user set name = #{name}, gender = #{gender} where id = #{id}")
    int updateUser(User user);

    @Delete("delete from user where id = #{id}")
    int deleteUserById(Integer id);

    @Insert("insert into user_role (user_id, role_id) values (#{userId}, #{roleId})")
    int insertUserRole(@Param("userId") Integer userId, @Param("roleId") Integer roleId);

    @Delete("delete from user_role where user_id = #{userId}")
    int deleteUserRole(Integer userId);

    @Select("select user_information_id from user where id = #{userId}")
    Integer selectUserInforIdByUserId(Integer userId);
}
